package com.orange.modules.dao;

import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.orange.dbhelp.DbHelper;
import com.orange.modules.entity.AuthSchoolItemMap;

@Repository
public class AuthSchoolItemMapRepository {

	@Resource(name = "dbHelper")
	private DbHelper dbHelper;
	
	@Autowired
	private AuthSchoolItemMapDao authSchoolItemMapDao;
	
	/**
	 * 新增自评信息
	 * @param asim
	 * @throws SQLException
	 */
	public void insertSelfEvaluate(AuthSchoolItemMap asim) throws SQLException{
		authSchoolItemMapDao.save(asim);
	}
	
	/**
	 * 删除自评信息
	 * @param schoolId
	 * @param subject
	 * @throws SQLException
	 */
	public void deleteSelfEvaluate(String itemId ,String schoolId,String subject) throws SQLException{
		String sql="DELETE FROM t_auth_school_item_map WHERE item_id=? AND  s_id=?";
		if(StringUtils.isNotBlank(subject)){
			sql+=" AND　login_type= '"+subject+"'";
		}
		dbHelper.execute(sql,itemId, schoolId);
	}
	
	
	public AuthSchoolItemMap queryAuthSchoolItemByPid(String pid) throws SQLException{
		String sql = "SELECT t.p_id as pid,t.s_id as sid,t.item_id as itemid,t.s_reason as reason,t.s_createtime as createtime,t.login_type as logintype,t.s_singlelevel as singlelevel,t.s_selfinstr as selfinstr "
				+ "  FROM  t_auth_school_item_map t";
		return null;
	}
	
	/**
	 * 根据分项ID查询该分项的数据
	 * @param itemId
	 * @return
	 * @throws SQLException
	 */
	public AuthSchoolItemMap querySchoolItemByItemId(String itemId,String schoolId,String subject) throws SQLException{
		String sql = "SELECT t.p_id as pid,t.s_id as sid,t.item_id as itemid,t.s_reason as reason,t.s_createtime as createtime,t.login_type as logintype,t.s_singlelevel as singlelevel,t.s_selfinstr as selfinstr "
				+ " FROM t_auth_school_item_map t WHERE t.item_id =? and t.s_id =?";
		if(StringUtils.isNotBlank(subject)){
			sql+=" AND t.login_type='"+subject+"'";
		}
		AuthSchoolItemMap asim = dbHelper.getBean(sql, AuthSchoolItemMap.class, itemId,schoolId);
		return asim;
	}
	
	/**
	 * 查询二级指标是否被填报过（如果填报过 list>0）
	 * @param schoolId
	 * @param loginType
	 * @param itemId
	 * @return
	 * @throws SQLException
	 */
	public List<AuthSchoolItemMap> querySchoolItemBySchoolId(String schoolId,String loginType,String itemId) throws SQLException{
		String sql = "SELECT t.p_id as pid,t.s_id as sid,t.item_id as itemid,t.s_reason as reason,t.s_createtime as createtime,t.login_type as logintype,t.s_singlelevel as singlelevel,t.s_selfinstr as selfinstr "
				+ " FROM t_auth_school_item_map t WHERE t.s_id = '"+schoolId+"' AND t.login_type = '"+loginType+"' AND t.item_id = '"+itemId+"'";
		List<AuthSchoolItemMap> list = dbHelper.getBeanList(sql, AuthSchoolItemMap.class, null);
		return list;
	}
	
	
	/**
	 * 查询二级指标是否被填报过（如果填报过 list>0）
	 * @param schoolId
	 * @param loginType
	 * @param itemId
	 * @return
	 * @throws SQLException
	 */
	public List<AuthSchoolItemMap> querySchoolItemBySchoolId(String schoolId,String loginType) throws SQLException{
		String sql = "SELECT t.p_id as pid,t.s_id as sid,t.item_id as itemid,t.s_reason as reason,t.s_createtime as createtime,t.login_type as logintype,t.s_singlelevel as singlelevel,t.s_selfinstr as selfinstr "
				+ " FROM t_auth_school_item_map t WHERE t.s_id = '"+schoolId+"' AND t.login_type = '"+loginType+"'";
		List<AuthSchoolItemMap> list = dbHelper.getBeanList(sql, AuthSchoolItemMap.class, null);
		return list;
	}
}
